﻿using Sys.Common;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Core.Sys
{
    /// <summary>
    /// 菜单按钮业务
    /// </summary>
    public class MenuButtonBll : ExampleBase<SysMenuButton, MenuButtonBll>
    {
        /// <summary>
        /// 排序实体
        /// </summary>
        public IQueryable<SysMenuButton> SortEntities
        {
            get { return Entities.OrderBy(m => m.IntSort); }
        }

        /// <summary>
        /// 根据菜单编号获取列表
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <returns>实体列表</returns>
        public IQueryable<SysMenuButton> EntitiesByMenuID(string id)
        {
            return SortEntities.Where(m => m.MenuID.Equals(id));
        }

        /// <summary>
        /// 分配按钮
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <param name="list">分配的数据集合</param>
        /// <returns>操作结果</returns>
        public bool Assign(string id, List<SysMenuButton> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ID = StringHelper.GetGuid();
                list[i].IntSort = i;

                list[i].SysMenu = null;
                list[i].SysButton = null;
            }

            Delete(m => m.MenuID.Equals(id));

            return Insert(list) == list.Count;
        }
    }
}
